package cn.achuan.spring.springcloud.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.bus.event.RemoteApplicationEvent;
import org.springframework.context.ApplicationContext;


/**
 * 远程事件发布者
 */
public class RemoteApplicationEventPublisher {
    private static Logger logger = LoggerFactory.getLogger(RemoteApplicationEventPublisher.class);

    /**
     * 发布一个事件
     * @param event
     */
    public static void publishEvent(RemoteApplicationEvent event){
        ApplicationContext context = ApplicationContextHolder.getApplicationContext();
        if(context != null) {
            context.publishEvent(event);
            logger.debug("已发布事件:{}", event);
        }else{
            logger.warn("未获取到当前Spring上下文信息，无法发布事件");
        }
    }
}